package github.fafaovo.common.event;

import github.fafaovo.common.config.exampleconfig;
import github.fafaovo.common.mycapability.ISpeedUpCapability;
import github.fafaovo.core.registry.CommonSetupEventHandler;
import github.fafaovo.example;
import github.fafaovo.network.NetworkRegistryHandler;
import github.fafaovo.network.Packet.PowerClinetForServer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class JoinWorldEvent {
    public static int count = 0;
    @SubscribeEvent
    public static void onJoinWorld(TickEvent.PlayerTickEvent event)
    {
        if(event.phase == TickEvent.Phase.START && event.side.isClient())
        {
            count++;
            if(count > 100)
            {
                count = 0;
                int amount = exampleconfig.example_integer.get();
                //int amount = 0;
                PlayerEntity playerEntity = event.player;
                LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
                capability.ifPresent((c)->{
                    float examplePower = c.getLevel();
                    if(exampleconfig.example_boolean.get())
                    {
                        c.setLevel(examplePower + amount);
                    }
                    else
                    {
                        if(examplePower - amount <= 0)
                            c.setLevel(0);
                        else
                            c.setLevel(examplePower - amount);
                    }

                    NetworkRegistryHandler.CHANNEL.sendToServer(new PowerClinetForServer(c.getLevel()));
                });
            }
        }


    }

}
